import Vue from 'vue';
import qs from 'qs'
const rootPath = '/health-box-api/v1'
const hospitalPrefix = rootPath + '/organs';
const limitPrefix = rootPath + '/appointments/limit';
const infoPrefix = rootPath + "/appointments/info";
/**
 * 查询所有检测机构
 * (获取所有检测机构,默认搜索全部，可以通过检测机构名称和手机号搜索，支持模糊查询(使用like可以少输入没关系，输错就找不到))
 * 
 * @returns 
 */
export const getHospitals = () => {
    return Vue.$http({
        url: hospitalPrefix,
        method: 'get'
    });
};

/**
 * 添加检测机构
 * @param  data 
 * {
  "hospitalId": null,
  "address": "string",
  "name": "string",
  "phone": "string"
} 
 * @returns 
 */
export const addHospital = (data) => {
    data.hospitalId = null;
    return Vue.$http({
        url: hospitalPrefix,
        method: 'post',
        data: data
    })
}

/**
 * 修改检测机构
 * @param  data 
 * {
  "hospitalId": Number,
  "address": "string",
  "name": "string",
  "phone": "string"
} 
 * @returns 
 */
export const modifyHospital = (data) => {
    return Vue.$http({
        url: hospitalPrefix,
        method: 'put',
        data: data
    })
}

/**
 * 获取可预约机构信息
 * @param {Number} hospitalId 
 * @returns 
 */
export const getLimits = (hospitalId)=>{
    return Vue.$http({
        url: limitPrefix + "/?hospitalId=" + hospitalId,
        method: "get"
    })
}

/**
 * 添加新可检测时间段
 * @param {} limit 
 * @returns 
 */
export const addLimit = (limit)=>{
    Date.prototype.format = function(format) {
        var date = {
            "M+": this.getMonth() + 1,
            "d+": this.getDate(),
            "h+": this.getHours(),
            "m+": this.getMinutes(),
            "s+": this.getSeconds(),
            "q+": Math.floor((this.getMonth() + 3) / 3),
            "S+": this.getMilliseconds()
        };
        if (/(y+)/i.test(format)) {
            format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
        }
        for (var k in date) {
            if (new RegExp("(" + k + ")").test(format)) {
                format = format.replace(RegExp.$1, RegExp.$1.length == 1 ?
                    date[k] : ("00" + date[k]).substr(("" + date[k]).length));
            }
        }
        return format;
    }

    var limitDTO = {
        hospitalId: limit.hospitalId,
        date: limit.date.format("yyyy-MM-dd"),
        startTime: limit.startTime + ":00",
        endTime: limit.endTime + ":00",
        maxNumber: limit.maxNumber
    }
    console.log(limitDTO);
    return Vue.$http({
        url: limitPrefix,
        data: limitDTO,
        method: "post"
    })
}

/**
 * 修改可检测时间段
 * @param {} limit 
 * @returns 
 */
export const modifyLimit = (limit)=>{
    Date.prototype.format = function(format) {
        var date = {
            "M+": this.getMonth() + 1,
            "d+": this.getDate(),
            "h+": this.getHours(),
            "m+": this.getMinutes(),
            "s+": this.getSeconds(),
            "q+": Math.floor((this.getMonth() + 3) / 3),
            "S+": this.getMilliseconds()
        };
        if (/(y+)/i.test(format)) {
            format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
        }
        for (var k in date) {
            if (new RegExp("(" + k + ")").test(format)) {
                format = format.replace(RegExp.$1, RegExp.$1.length == 1 ?
                    date[k] : ("00" + date[k]).substr(("" + date[k]).length));
            }
        }
        return format;
    }

    var limitDTO = {
        hospitalId: limit.hospitalId,
        date: limit.date.format("yyyy-MM-dd"),
        startTime: limit.startTime + ":00",
        endTime: limit.endTime + ":00",
        maxNumber: limit.maxNumber
    }
    console.log(limitDTO);
    return Vue.$http({
        url: limitPrefix,
        data: limitDTO,
        method: "post"
    })
}

/**
 * 删除可预约机构信息
 * @param {Number} hospitalId 
 * @returns 
 */
 export const deleteLimit = (limitId)=>{
    return Vue.$http({
        url: limitPrefix + "/" + limitId,
        method: "delete"
    })
}


/**
 * 查询可预约记录
 * @param {
 * date:想要预约的日期
 * hospitalId: 想要预约机构
 * pageindex: 如果查询结果多余该数目返回第几页
 * pageSize: 一次返回的最大数目
 * } queryParam 
 * @returns 
 */
export const getValidAppointments = (queryParam)=>{
    console.log(qs.stringify(queryParam))
    return Vue.$http({
        url: infoPrefix + "?" + qs.stringify(queryParam),
        method: "get"
    })
}

/**
 * 用户查看历史预约信息列表
 * @returns 
 */
 export const getHistoryAppointments = ()=>{
    return Vue.$http({
        url: infoPrefix + "/histories",
        method: "get"
    })
}

/**
 * 添加预约
 * @param {Number} limitId 
 * @returns 
 */
export const addAppointment = (limitId)=>{
    return Vue.$http({
        url: infoPrefix + "/?aLId=" + limitId,
        method: "put"
    })
}